﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Davsha
{
    class DeviceInfo
    {
        public DeviceInfo(int vendorId, int productId)
        {
            VendorId = vendorId;
            ProductId = productId;
        }

        public int VendorId
        {
            get;
            set;
        }

        public int ProductId
        {
            get;
            set;
        }


        public static DeviceInfo FromDeviceName(string deviceName)
        {
            Match m = Regex.Match(deviceName, "VID_(?<VendorId>[0-9a-fA-F]+)&PID_(?<ProductId>[0-9a-fA-F]+)");

            if (!m.Success)
            {
                throw new Exception("invalid device name syntax: " + deviceName);
            }

            int vendorId = Convert.ToInt32(m.Groups["VendorId"].Value, 16);
            int productId = Convert.ToInt32(m.Groups["ProductId"].Value, 16);

            return new DeviceInfo(vendorId, productId);
        }
    }
}
